﻿using System;
using SNSGame.Net;

namespace SNSGame.SNSGame
{
    /// <summary>
    /// 游戏应用接口基类
    /// zhouyu,2011-08-13 created.
    /// </summary>
    [Obsolete("请使用改进版本的AppApiBase，已经实现了Url上附加参数与Post参数分离。")]
    public abstract  class ApiBase
    {
        private static AppConfigBase _appConfig;
        /// <summary>
        /// 应用配置信息
        /// </summary>
        protected AppConfigBase AppConfig
        {
            get { return _appConfig; }
            set { _appConfig = value; }
        }

        /// <summary>
        /// 时间戳,不满意自已重写吧
        /// </summary>
        protected virtual long TimeStamp
        {
            get { return DateTime.Now.Ticks; }
        }

        /// <summary>
        /// 授权基础参数集合
        /// 会自动加入到请求的参数集合中
        /// </summary>
        protected abstract HttpParams AuthParamters { get; }
        
        /// <summary>
        /// 在原参数基础上附加上参数签名，必须有子类实现。父类不管参数的样子及签名算法的实现。
        /// Note:未对签名参数进行判断，因此不要传入了同名的签名参数。
        /// </summary>
        /// <param name="httpParams">相关参数集合<see cref="HttpParams"/></param>
        /// <returns></returns>
        protected abstract void AppendSignature(HttpParams httpParams);


        #region 同步Http方法

        /// <summary>
        /// Http的Get方法实现
        /// </summary>
        /// <param name="uri">请求的URL</param>
        /// <returns>请求结果</returns>
        protected virtual string HttpGet(string uri)
        {
            return HttpGet(uri, null, AppConfig.Timeout);
        }

        /// <summary>
        /// Http的Get方法实现
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="timeout">超时，耗秒</param>
        /// <returns></returns>
        protected virtual string HttpGet(string uri,int timeout)
        {
            return HttpGet(uri, null, timeout);
        }

        /// <summary>
        /// Http的Get方法实现
        /// </summary>
        /// <param name="uri"></param>
        /// <param name="urlParameters"></param>
        /// <returns></returns>
        protected virtual string HttpGet(string uri,HttpParams urlParameters)
        {
            return HttpGet(uri, urlParameters, AppConfig.Timeout);
        }

        /// <summary>
        /// Http的Get方法实现
        /// </summary>
        /// <param name="uri">基础Uri,即?号前的URL部分</param>
        /// <param name="urlParameters">附加参数</param>
        /// <param name="timeout">超时时间，耗秒</param>
        /// <returns>请求结果</returns>
        protected  virtual string HttpGet(string uri,HttpParams urlParameters,int timeout)
        {
            if(string.IsNullOrEmpty(uri) || uri.IndexOf("?")>=0 )
                throw new ArgumentException("uri参数值只能为Url中?号以前的部分。", "uri");
            var qparam = AuthParamters;
            if(urlParameters!=null)
                qparam.AddRange(urlParameters);
            AppendSignature(qparam);
            var url = string.Format("{0}?{1}", uri, qparam);
            var rst = HttpHelper.Get(url,timeout);
            return rst;
        }

        /// <summary>
        /// Http的Post方法实现
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        protected virtual string HttpPost(string url)
        {
            return HttpPost(url, null, AppConfig.Timeout);
        }

        /// <summary>
        /// Http的Post方法实现
        /// </summary>
        /// <param name="url"></param>
        /// <param name="timeout">超时，毫秒</param>
        /// <returns></returns>
        protected virtual  string HttpPost(string url,int timeout)
        {
            return HttpPost(url, null, timeout);
        }
        /// <summary>
        /// Http的Post方法实现
        /// </summary>
        /// <param name="url"></param>
        /// <param name="urlParameters"></param>
        /// <returns></returns>
        protected virtual string HttpPost(string url,HttpParams urlParameters)
        {
            return HttpPost(url, urlParameters, AppConfig.Timeout);
        }

        /// <summary>
        /// Http的Post方法实现
        /// </summary>
        /// <param name="url">请求的URL</param>
        /// <param name="urlParameters">要Post的数据，键值集合，<see cref="HttpParams"/></param>
        /// <param name="timeout">超时，毫秒</param>
        /// <returns></returns>
        protected virtual  string HttpPost(string url,HttpParams urlParameters,int timeout)
        {
            var qparam = AuthParamters;
            if(urlParameters!=null)
                qparam.AddRange(urlParameters);
            AppendSignature(qparam);
            var rst = HttpHelper.Post(url, qparam, timeout);
            return rst;
        }

        #endregion


        #region  异步Async Http方法

        protected virtual bool AsyncHttpGet(string uri,AsyncHttpCallback callback)
        {
            return AsyncHttpGet(uri, null, callback);
        }

        protected  virtual  bool AsyncHttpGet(string uri,HttpParams urlParams,AsyncHttpCallback callback)
        {
            if (string.IsNullOrEmpty(uri) || uri.IndexOf("?") >= 0)
                throw new ArgumentException("uri参数值只能为Url中?号以前的部分。", "uri");
            var qparam = AuthParamters;
            if (urlParams != null)
                qparam.AddRange(urlParams);
            AppendSignature(qparam);
            var url = string.Format("{0}?{1}", uri, qparam);
            var rst = HttpHelper.AsyncGet(url, AppConfig.AsyncTimeout,callback);
            return rst;
        }

        protected  virtual  bool AsyncHttpPost(string url,AsyncHttpCallback callback)
        {
            return AsyncHttpPost(url, null, callback);
        }

        protected virtual bool AsyncHttpPost(string url,HttpParams urlParams,AsyncHttpCallback callback)
        {
            var qparam = AuthParamters;
            if (urlParams != null)
                qparam.AddRange(urlParams);
            AppendSignature(qparam);
            var rst = HttpHelper.AsyncPost(url, qparam, AppConfig.AsyncTimeout,callback);
            return rst;
        }

        #endregion

    }
}